﻿var jQuery;
var tempId = "0";
var refId = 0;
var embed_call_payment;
var popup_payment;
function byhand_loadJquery(finalexec) {
    // Localize jQuery variable
    /******** Load jQuery if not present *********/
    if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.10.2') {
        var script_tag = document.createElement('script');
        script_tag.setAttribute("type", "text/javascript");
        script_tag.setAttribute("src",
            "http://byhand.vn/content/js/jquery.min.js");

        if (script_tag.readyState) {
            script_tag.onreadystatechange = function () { // For old versions of IE
                if (this.readyState == 'complete' || this.readyState == 'loaded') {
                    scriptLoadHandler(jQuery);
                }
            };
        } else {
            script_tag.onload = scriptLoadHandler;
        }
        // Try to find the head, otherwise default to the documentElement
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
    } else {
        // The jQuery version on the window is the one we want to use
        jQuery = window.jQuery;
        finalexec();
    }
    /******** Called once jQuery has loaded ******/
    function scriptLoadHandler() {
        // Restore $ and window.jQuery to their previous values and store the
        // new jQuery in our local jQuery variable
        jQuery = window.jQuery;
        // Call our main function
        finalexec();
    }
}
function byhand_finalexec() {
    jQuery(document).ready(function ($) {
        if (tempId == "0") {
            tempId = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);
            });
        }

        if (!$("link[href='http://byhand.vn/content/css/bootstrapbuttonembed.min.css']").length) {
            (function (d, t) {
                t = d.createElement('link');
                t.setAttribute('href', 'http://byhand.vn/content/css/bootstrapbuttonembed.min.css');
                t.setAttribute('rel', 'stylesheet');
                d.getElementsByTagName('head')[0].appendChild(t)
            })(document)
        }
        if (!$("script[src='http://byhand.vn/content/js/bootstrap.min.js']").length) {
            (function (d, t) { t = d.createElement('script'); t.setAttribute('src', 'http://byhand.vn/content/js/bootstrapbuttonembed.min.js'); d.getElementsByTagName('head')[0].appendChild(t) })(document)
        }

        embed_call_payment = function (that, productid, tempID) {
            //check if use button embled html/js
            var refid = $(that).attr('data-refid');
            popup_payment(productid, refid);
        }
        popup_payment = function (id, refid) {
            var size = {
                width: window.innerWidth || document.body.clientWidth,
                height: window.innerHeight || document.body.clientHeight
            }
            if (size.width <= 600) {
                window.location = 'http://byhand.vn/Payment/WidgetPopup?location=' + top.location.href + '&opentype=1&id=' + id + '&refid=' + refid + '&tempID=' + tempId;
                return;
            }

            var container_payment;
            container_payment = $('.byhand-container-frame[data-id=' + id + ']');
            if (container_payment.length === 0) {
                $('body').append('<div class="modal fade byhand-container-frame" data-id="' + id + '" > <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title">Mua hàng tại ByHand.vn</h4> </div><div class="byhand-wrapper-widgetpopup"></div></div> </div> </div>');
                //$('body').append('<div class="byhand-container-frame" data-id="' + id + '" style="height: auto; width: auto;"></div>');
                container_payment = $('.byhand-container-frame[data-id=' + id + ']');
                $(container_payment).find('.byhand-wrapper-widgetpopup').html('<iframe src="http://byhand.vn/Payment/WidgetPopup?location=' + top.location.href + '&opentype=1&id=' + id + '&refid=' + refid + '&tempID=' + tempId + '" width="578" height="570" frameborder="0" scrolling="auto" id="the_iframe" height="auto"></iframe>');
                //$(container_payment).dialog({ width: 750, height: 800, closeOnEscape: true, modal: true, resizable: false });
                $(container_payment).modal({ keyboard: true });
            } else {
                var content = $(container_payment);
                //$(content).dialog();
                $(content).modal({ keyboard: true });
            }
        }
    });
}
(function () {
    byhand_loadJquery(byhand_finalexec);
})();